/*
Copyright (c) 2013, Sten Lindgren
All rights reserved.

Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:

- Redistributions of source code must retain the above copyright notice, this
  list of conditions and the following disclaimer.

- Redistributions in binary form must reproduce the above copyright notice,
  this list of conditions and the following disclaimer in the documentation
  and/or other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/


#include <ctype.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <strings.h>

#include "lexer.h"


int	parse_sock;		/* socket to be used by parser to get chars */
char	next_token_char;	/* next char to parse */
token_t	next_token;		/* description of next token during parse */

char	*token_type_name[] =
		{
		"none",
		"char",
		"end of line",
		"number",
		"string",
		"identifier"
		};


/* Get next char from socket, to be used only by tokenizer.
   Freeze input at end if line. */

void parse_next_char(void)
	{
	char	ch;

	if ((next_token_char == '\n') || (next_token_char == '\r'))
		return;

	if (read(parse_sock, &ch, 1) == 1)
		{
		next_token_char = ch;
		return;
		}

	next_token_char = 0;
	}


/* force consuming input at end of line */

void parse_skip_end_of_line(void)
	{
	next_token_char = 0;
	parse_next_char();
	}


/* parse a number, assuming first char is already verified to be a digit */

void parse_next_token_number(void)
	{
	int	length;

	length = 0;
	while (isdigit(next_token_char) && (length < TOKEN_MAX))
		{
		next_token.chars[length] = next_token_char;
		parse_next_char();
		length++;
		}
	next_token.chars[length] = 0;

	next_token.type		= TOKEN_TYPE_NUMBER;
	next_token.number	= atoi(next_token.chars);
	}


/* parse string, assuming first char is already verified to be a delimiter */

void parse_next_token_string(void)
	{
	int	length;
	char	delimiter;

	delimiter = next_token_char;
	parse_next_char();

	length = 0;
	while (next_token_char && (length < TOKEN_MAX) &&
	       (next_token_char != delimiter))
		{
		next_token.chars[length] = next_token_char;
		parse_next_char();
		length++;
		}
	next_token.chars[length] = 0;

	if (next_token_char == delimiter)
		parse_next_char();

	next_token.type = TOKEN_TYPE_STRING;
	}


/* parse identifier, assuming first char is already verified to be valid */

void parse_next_token_ident(void)
	{
	int	length;

	length = 0;
	while (isalnum(next_token_char) && (length < TOKEN_MAX))
		{
		next_token.chars[length] = next_token_char;
		parse_next_char();
		length++;
		}
	next_token.chars[length] = 0;

	next_token.type = TOKEN_TYPE_IDENT;

	if (!strcasecmp(next_token.chars, "bell"))
		next_token.ident = TOKEN_IDENT_BELL;
	else if (!strcasecmp(next_token.chars, "dim"))
		next_token.ident = TOKEN_IDENT_DIM;
	else if (!strcasecmp(next_token.chars, "exit"))
		next_token.ident = TOKEN_IDENT_EXIT;
	else if (!strcasecmp(next_token.chars, "events"))
		next_token.ident = TOKEN_IDENT_EVENTS;
	else if (!strcasecmp(next_token.chars, "off"))
		next_token.ident = TOKEN_IDENT_OFF;
	else if (!strcasecmp(next_token.chars, "on"))
		next_token.ident = TOKEN_IDENT_ON;
	else
		next_token.ident = TOKEN_IDENT_NONE;
	}


/* parse single character not matching any other token type */

void parse_next_token_char(void)
	{
	next_token.type = TOKEN_TYPE_CHAR;
	parse_next_char();
	}


/* parse next token and leave result in next_token struct */

void parse_next_token(void)
	{
	if (next_token.type == TOKEN_TYPE_END_OF_LINE)
		parse_skip_end_of_line();

	/* clear token struct */
	memset(&next_token, 0, sizeof(next_token));

	/* skip white space */
	while (next_token_char && (next_token_char <= ' '))
		{
		/* detected end of line */
		if ((next_token_char == '\n') || (next_token_char == '\r'))
			{
			next_token.type		= TOKEN_TYPE_END_OF_LINE;
			next_token.chars[0]	= next_token_char;

			return;
			}

		/* skip normal white space */
		parse_next_char();
		}

	if (!next_token_char)
		next_token.type = TOKEN_TYPE_NONE;
	else if (isdigit(next_token_char))
		parse_next_token_number();
	else if ((next_token_char == '"') || (next_token_char == '\''))
		parse_next_token_string();
	else if (isalnum(next_token_char))
		parse_next_token_ident();
	else
		parse_next_token_char();
	}


/* prepare parser and get first token */

void parse_start(int sock)
	{
	parse_sock = sock;
	parse_next_char();
	parse_next_token();
	}
